package com.xbn.xbnmall.order.interceptor;

import com.xbn.common.constant.AuthServerConstant;
import com.xbn.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author XBN
 * @date 2020/8/17 17:37
 * @email 78742541@qq.com
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor {
        public  static  ThreadLocal<MemberRespVo> loginUser=new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean match = new AntPathMatcher().match("/order/order/status/**", request.getRequestURI());
        if(match)
        {
            return true;
        }
        MemberRespVo memberRespVo = (MemberRespVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER_SESSION_KEY);
        if(memberRespVo!=null)
        {
            loginUser.set(memberRespVo);
            return true;
        }
        else {
            //没登陆就去登陆
            response.sendRedirect("http://auth.xbnmall.com/login.html");
            return false;
        }


    }
}
